import axios from 'axios'
import store from '../store'
import { ACCESS_TOKEN } from '../store/mutation-types'
//import storage from 'store'
import qs from 'qs'
import NProgress from 'nprogress' 
import 'nprogress/nprogress.css' 
import { Message } from 'element-ui';

//NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })
NProgress.configure({ showSpinner: false })


axios.defaults.baseURL = process.env.VUE_APP_API_URL;

if(process.env.NODE_ENV=='development'){
    //axios.defaults.headers.HTTP_X_USER_ID='D9376F28-1EBA-4293-9BCC-96A9C5C4C627';
}


// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    let token = $cookies.get("HTTP_X_TOKEN")//storage.get(ACCESS_TOKEN)
    if (token) {
        config.headers['HTTP-X-TOKEN'] = token
    }
    if(config.method=='get'){
        NProgress.start();
    }
    return config;
}, function (error) {
    NProgress.done();
    // 对请求错误做些什么
    Message.error(error)
    return Promise.reject(error);
});
//
// // 添加响应拦截器
axios.interceptors.response.use(function (response) {
    NProgress.done();
    if(response.data && response.data.code==401){
        store.dispatch('Logout').then(() => {
            Message.error({key:'logout',content:"登陆超时，请退出后重新登录！",duration:3,onClose:()=>{
                window.location.reload()
            }})
        })
        this.$router.push({ path: '/login' })
        return ;
    }
    if(!response.data){
        response.data={data:null}
        return response
    }
    if(response.data.code!=200){
        Message.error(response.data.msg)
    }
    return response;
}, function (error) {
    NProgress.done();
    // 对响应错误做点什么
    if(error.response) {
        Message.error(`错误code=${error.response.status}`)

    }
    return Promise.reject(error);
});


function get(url,params = {},headers={}) {
    return new Promise((resolve,reject)=>{
        axios.get(url,{params:params},{headers:{"Content-Type":"application/x-www-form-urlencoded"},...headers})
            .then(response => {
                // console.log(response);
                // response&&response.data && response.data.data?resolve(response.data.data):resolve(null);
                response&&response.data?resolve(response.data):resolve(null);
            })
            .catch(err => {
                reject(err);
            })
    })
}


function post(url,params = {},headers={}){
    return new Promise((resolve,reject)=>{
        axios.post(url,qs.stringify(params),{headers:{"Content-Type":"application/x-www-form-urlencoded"},...headers})
            .then(response => {
                // console.log(response);
                // response&&response.data && response.data.data?resolve(response.data.data):resolve(null);
                response&&response.data?resolve(response.data):resolve(null);
            })
            .catch(err => {  
                reject(err);
            })
    })
}
function postjson(url,params = {},headers={}){
    return new Promise((resolve,reject)=>{
        axios.post(url,params,{headers:{"Content-Type":"application/json"},...headers})
            .then(response => {
                // console.log(response);
                // response&&response.data && response.data.data?resolve(response.data.data):resolve(null);
                response&&response.data?resolve(response.data):resolve(null);
            })
            .catch(err => {
                reject(err);
            })
    })
}
export default axios
export {get, post, postjson}